/*
 * Wantmore - Service Delivery Platform.
 *
 * $Id: FileUpoadController.java 63 2010-11-11 15:14:21Z alexey.batyuta@gmail.com $
 *
 * Copyright 2010 by Sunbay Software Crimea Ltd.
 * All rights reserved
 */
package ua.crimea.bat.ss.forms;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.SimpleFormController;
import ua.crimea.bat.ss.model.beans.Image;
import ua.crimea.bat.ss.views.JpegView;
import ua.crimea.bat.ss.services.PhotoAlbumsService;

import java.util.HashMap;
import java.util.Map;

/**
 * User: bat
 * Date: Mar 31, 2010
 * Time: 3:30:52 PM
 */
@Controller("fileUpoadController")
public class FileUpoadController extends SimpleFormController implements org.springframework.web.servlet.mvc.Controller{
   protected PhotoAlbumsService photoAlbumsService;

    public FileUpoadController() {
    }

    public void setPhotoAlbumsService(PhotoAlbumsService photoAlbumsService) {
        this.photoAlbumsService = photoAlbumsService;
    }

    public PhotoAlbumsService getPhotoAlbumsService() {
        return photoAlbumsService;
    }

    @InitBinder
    public void setAllowedFields(WebDataBinder dataBinder) {
        dataBinder.setDisallowedFields("id");
    }

    @Autowired
    public FileUpoadController(PhotoAlbumsService photoAlbumsService) {
        setPhotoAlbumsService(photoAlbumsService);
    }

    @RequestMapping(value = "/albums/images/{imageId}",method = RequestMethod.GET)
    protected ModelAndView handleRequestInternal(@PathVariable("imageId") int imageId) throws Exception {
        ModelAndView modelAndView = new ModelAndView();
        Image image = this.photoAlbumsService.loadImage(imageId);
        JpegView view = new JpegView();
        Map<String, Object> map =  new HashMap();
        map.put("imageData",image.getImage());
        view.setAttributesMap(map);
        modelAndView.setView(view);
        return modelAndView;
        //todo:
	}
}

